﻿// Copyright 2001-2010 - Active Up SPRLU (http://www.agilecomponents.com)
//
// Data Connection Suite is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.

// Data Connection Suite is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.

// You should have received a copy of the GNU General Public License along with
// Data Connection Suite. If not, see http://www.gnu.org/licenses/.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace AgileComponents.DataConnection
{
    public partial class OracleDataConnectionControl : DataConnectionControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="OracleDataConnectionControl"/> class.
        /// </summary>
        public OracleDataConnectionControl()
        {
            InitializeComponent();

            this.Builder = new OracleConnectionStringBuilder();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Click event of the testConnectionButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void testConnectionButton_Click(object sender, EventArgs e)
        {
            this.TestConnectionString(this.serverNamesComboBox.Text);
        }

        /// <summary>
        /// Handles the Click event of the advancedButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void advancedButton_Click(object sender, EventArgs e)
        {
            ConnectionStringPropertiesDialog dialog = new ConnectionStringPropertiesDialog(this.Builder);
            dialog.ShowDialog();

            PopulateForm();
        }

        /// <summary>
        /// Handles the TextChanged event of the serverNamesComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void serverNamesComboBox_TextChanged(object sender, EventArgs e)
        {
            this.RaiseDataConnectionChanged();
        }

        /// <summary>
        /// Handles the TextChanged event of the usernameTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void usernameTextBox_TextChanged(object sender, EventArgs e)
        {
            this.RaiseDataConnectionChanged();
        }

        /// <summary>
        /// Handles the TextChanged event of the passwordTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
            this.RaiseDataConnectionChanged();
        }

        #endregion

        #region Implementation

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <returns></returns>
        public override IDbConnection GetConnection()
        {
            return new OracleConnection(this.ConnectionString);
        }

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public override string ConnectionString
        {
            get
            {
                PopulateBuilder();
                return base.ConnectionString;
            }
            set
            {
                base.ConnectionString = value;
                PopulateForm();
            }
        }

        /// <summary>
        /// Populates the builder.
        /// </summary>
        private void PopulateBuilder()
        {
            OracleConnectionStringBuilder builder = (OracleConnectionStringBuilder)Builder;

            builder.DataSource = this.serverNamesComboBox.Text;
            builder.UserID = this.usernameTextBox.Text;
            builder.Password = this.passwordTextBox.Text;
        }

        /// <summary>
        /// Populates the form.
        /// </summary>
        private void PopulateForm()
        {
            OracleConnectionStringBuilder builder = (OracleConnectionStringBuilder)Builder;

            this.serverNamesComboBox.Text = builder.DataSource;
            this.usernameTextBox.Text = builder.UserID;
            this.passwordTextBox.Text = builder.Password;
        }

        #endregion
    }
}
